package leetcode2;

import java.util.Arrays;

public class CountFairPairs {
    public long countFairPairs(int[] nums, int lower, int upper) {
        int i = 0, end = nums.length - 1;
        long res = 0;
        Arrays.sort(nums);
        while (i < end) {
            for (int j = end; j > i; j--) {
                int sum = nums[i] + nums[j];
                if (sum < lower) {
                    i++;
                } else if (sum > upper) {
                    end--;
                } else {
                    res++;
                    i++;
                }
            }
//            i++;
        }
        return res;
    }

    public static void main(String[] args) {
        CountFairPairs countFairPairs = new CountFairPairs();
        System.out.println(countFairPairs.countFairPairs(new int[]{0, 1, 7, 4, 4, 5}, 3, 6));
    }
}
